package summary;

import java.io.*;

/**
 * @Author admin
 * @Date 2019/4/12 9:03
 * 转换流：解决乱码问题
 */
public class Test3 {
    public static void main(String[] args) {
        /*1.创建转换流对象（注意一下编码格式）
        ANSI——GBK
        2.用转换流对象去构造字符缓冲流
        3.循环操作：
        读取源文件一行文本，写入目标文件
        增加换行符
        4.刷新
        5.关闭流
        * */
        /*转换流构造方法需要传递2个参数：
        基本字节流、编码格式
        * */
        InputStream in = null;
        InputStreamReader isr = null;
        BufferedReader br = null;

        OutputStream out = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            in = new FileInputStream("D:\\sfz1.txt");
            isr = new InputStreamReader(in, "GBK");
            br = new BufferedReader(isr);


            String line;
            out = new FileOutputStream("sfz1.txt");
            osw = new OutputStreamWriter(out, "UTF-8");
            bw = new BufferedWriter(osw);
            while ((line = br.readLine()) != null) {
                bw.write(line);
                bw.newLine();
            }
            bw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (isr != null) {
                    isr.close();
                }
                if (br != null) {
                    br.close();
                }
                if (out != null) {
                    out.close();
                }
                if (osw != null) {
                    osw.close();
                }
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
